#include <Windows.h>
#include <sstream>
#include "common.h"
#include "process_mgmt.h"

using namespace std;


string decToString(const UINT dInput)
{
    ostringstream os;
    os<<dInput;

    return os.str();
}

string decToHexString(const UINT dInput)
{
    ostringstream os;
    os<<hex<<"0x"<<uppercase<<dInput<<nouppercase<<dec;

    return os.str();
}

wstring stringToWc(const string &sInput)
{
    wstring output = L"";

    if(sInput != UNNAMED) {
        UINT maxWcSize = sInput.size()+SPACE_FOR_NULL;
        WCHAR *wOutput = new WCHAR[maxWcSize];
        if( wOutput != NULL && mbstowcs_s(NULL, wOutput, maxWcSize, sInput.c_str(), maxWcSize) == 0 ) {
            if( *wOutput != NULL )
                output = wstring(wOutput);
            delete[] wOutput;
        }
    }

    return output;
}

string wcToString(const WCHAR *const wInput)
{
    string output = "";

    if( wInput != NULL && *wInput != NULL ) {
        UINT maxCSize = wcslen(wInput)+SPACE_FOR_NULL;
        CHAR *cOutput = new CHAR[maxCSize];

        if( cOutput != NULL && wcstombs_s(NULL, cOutput, maxCSize, wInput, maxCSize) == 0 ) {
            if( *cOutput != NULL )
                output = string(cOutput);
            delete[] cOutput;
        }
    }

    return output;
}

BOOL isWcSame(const WCHAR *const wcComp_1, const WCHAR *const wcComp_2)
{
    UINT wcCompSize_1 = wcslen(wcComp_1);
    UINT wcCompSize_2 = wcslen(wcComp_2);
    
    if( wcCompSize_1 == wcCompSize_2 && wcsncmp(wcComp_1, wcComp_2, wcCompSize_1) == 0 )
        return TRUE;
    return FALSE;
}

FunctionSuccesFailure copyWCharString(WCHAR *wcDest, UINT const wcDestSize, const WCHAR *const wcSrc)
{
    static const WCHAR c_string_delimiter_wc = L'\0';
    FunctionSuccesFailure fresult = FUNCTION_FAILURE;
    const UINT wcSrcSize = wcslen(wcSrc)+SPACE_FOR_NULL;

    if( wcDest != NULL && wcSrc != NULL && wcDestSize >= wcSrcSize ) {
        *wcDest = c_string_delimiter_wc;
        if(*wcSrc == c_string_delimiter_wc)
            fresult = FUNCTION_SUCCESS;
        else if(wcsncpy_s(wcDest, wcDestSize, wcSrc, wcSrcSize) == 0)
            fresult = FUNCTION_SUCCESS;
        else { /* unsupported error case */}
    }
    
    return fresult;
}

BOOL isKeyDown(UINT key)
{
    static const SHORT filter = (SHORT)(1<<(CHAR_BIT*sizeof(SHORT)-1));

    if(GetAsyncKeyState(key) & filter)
        return TRUE;
    return FALSE;
}


UINT getPressedKeys()
{
    static const UINT first_key    = 0x01;
    static const UINT last_key     = 0x9F;
    static const UINT keys_to_scan = 2;
    UINT pressed_key = 0;

    for(UINT nb_key_pressed=0, i=first_key, k=last_key; i<k; ++i) {
        if(isKeyDown(i)) {
            pressed_key |= i;
            if(++nb_key_pressed == keys_to_scan)
                break;
        }
    }

    return pressed_key;
}

